# 数据类型
# 整形 布尔型  浮点数 字符串 列表 元组 字典 集合

# 猜数字游戏
# import random
# number = random.randint(1, 100)
# while True:
#     guess = int(input("请输入数字："))
#     if guess == number:
#         print("恭喜你猜对了")
#         break
#     elif guess > number:
#         print("你猜的数字太大了")
#     else:
#         print("你猜的数字太小了")
#
# 打印九九乘法表
# for i in range(1, 10):
#     for j in range(1, i+1):
#         print(i, "*", j, "=", i*j, end=" ")
#         print()

# 使用tutle画图 实现绘制正方形
# import turtle
# turtle.forward(100)
# turtle.right(90)
# turtle.forward(100)
# turtle.right(90)
# turtle.forward(100)
# turtle.right(90)
# turtle.forward(100)
# turtle.done()

# #使用tutle画图 实现绘制五角星
# import turtle
#
# # 设置画布和画笔
# screen = turtle.Screen()
# screen.setup(800, 500)
# screen.bgcolor("red")
#
# # 创建画笔
# pen = turtle.Turtle()
# pen.speed(5)
# pen.penup()

# 绘制五角星的函数
# def draw_star(pen, x, y, size, color="yellow"):
#     pen.goto(x, y)
#     pen.pendown()
#     pen.color(color)
#     pen.begin_fill()
#     for i in range(5):
#         pen.forward(size)
#         pen.right(144)
#     pen.end_fill()
#     pen.penup()
#
# # 绘制大五角星
# draw_star(pen, -200, 100, 60, "yellow")
#
# # 绘制四个小五角星
# draw_star(pen, -100, 150, 20, "yellow")
# draw_star(pen, -80, 120, 20, "yellow")
# draw_star(pen, -80, 90, 20, "yellow")
# draw_star(pen, -100, 60, 20, "yellow")
#
# # 隐藏画笔并完成绘制
# pen.hideturtle()
# turtle.done()

#Python基本语法复习
# 1. 数据类型
# 整型(int): 1, 2, -3
# 浮点型(float): 3.14, -2.5
# 布尔型(bool): True, False
# 字符串(str): "hello", 'world'
# 列表(list): [1, 2, 3]
# 元组(tuple): (1, 2, 3)
# 字典(dict): {"key": "value"}
# 集合(set): {1, 2, 3}

import  pygame
# 使用pygame模块
# 实现植物大战僵尸中僵尸动态向前行走的代码
# 图片路径：images/普通/1.png共22张照片
# import pygame
# import os
#
# # 初始化Pygame
# pygame.init()
#
# # 设置屏幕尺寸和标题
# screen = pygame.display.set_mode((800, 600))
# pygame.display.set_caption("Plants vs Zombies - Zombie Walk")
#
# # 加载僵尸行走动画的图片序列
# zombie_images = []
# for i in range(1, 23):  # 共计22张图片
#     image_path = f"images/普通/{i}.png"
#     if os.path.exists(image_path):
#         img = pygame.image.load(image_path)
#         zombie_images.append(img)
#     else:
#         print(f"警告: 找不到文件 {image_path}")
#
# # 动画参数设置
# current_frame = 0  # 当前播放帧数
# zombie_x = 800  # 僵尸初始x坐标(从右侧进入)
# frame_rate = 10  # 每秒播放帧数
# clock = pygame.time.Clock()
#
# # 主循环
# running = True
# while running:
#     for event in pygame.event.get():
#         if event.type == pygame.QUIT:
#             running = False
#
#     # 更新动画帧
#     current_frame = (current_frame + 1) % len(zombie_images)
#
#     # 更新僵尸位置(向左移动)
#     zombie_x -= 2
#
#     # 清除屏幕
#     screen.fill((135, 206, 235))  # 天空蓝背景
#
#     # 在指定位置绘制当前帧图片
#     screen.blit(zombie_images[current_frame], (zombie_x, 300))
#
#     # 刷新显示
#     pygame.display.flip()
#
#     # 控制帧率
#     clock.tick(frame_rate)
#
# pygame.quit()


# 使用pygame模块
# 实现锤子砸僵尸的积分游戏一个僵尸20积分,并且击败僵尸之后出现太阳的代码
# 僵尸图片路径:images/普通/1.png到22.png共22张图片
# 锤子图片路径:images/hammer.png
# 背景图片路径:images/beijing.jpg
# 太阳图片路径:images/sun/1.png到18.png共18张图片

import pygame
import os
import sys
import random

# 初始化pygame
pygame.init()

# 设置窗口大小
SCREEN_WIDTH, SCREEN_HEIGHT = 1000, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("锤子砸僵尸积分游戏")

# 加载背景图片
background_image = None
bg_path = os.path.join("images", "beijing.jpg")
if os.path.exists(bg_path):
    background_image = pygame.image.load(bg_path).convert()
    background_image = pygame.transform.scale(background_image, (SCREEN_WIDTH, SCREEN_HEIGHT))
else:
    print(f"警告: 找不到背景图片 {bg_path}")

# 加载锤子图片
hammer_image = None
hammer_path = os.path.join("images", "hammer.png")
if os.path.exists(hammer_path):
    hammer_image = pygame.image.load(hammer_path).convert_alpha()
    hammer_image = pygame.transform.scale(hammer_image, (40, 40))
else:
    print(f"警告: 找不到锤子图片 {hammer_path}")
    # 创建默认锤子
    hammer_image = pygame.Surface((40, 40))
    hammer_image.fill((139, 69, 19))  # 棕色

# 加载奖杯图片
trophy_image = None
trophy_path = os.path.join("images", "trophy_hi_res.png")
if os.path.exists(trophy_path):
    trophy_image = pygame.image.load(trophy_path).convert_alpha()
    trophy_image = pygame.transform.scale(trophy_image, (200, 200))
else:
    print(f"警告: 找不到奖杯图片 {trophy_path}")

# 加载失败图片
fail_image = None
fail_path = os.path.join("images", "ZombiesWon.jpg")
if os.path.exists(fail_path):
    fail_image = pygame.image.load(fail_path).convert_alpha()
    fail_image = pygame.transform.scale(fail_image, (200, 200))
else:
    print(f"警告: 找不到失败图片 {fail_path}")

# 加载小车图片
car_image = None
car_path = os.path.join("images", "车.png")
if os.path.exists(car_path):
    car_image = pygame.image.load(car_path).convert_alpha()
    # 调整小车图片大小
    car_image = pygame.transform.scale(car_image, (60, 60))
else:
    print(f"警告: 找不到小车图片 {car_path}")
    # 创建默认小车
    car_image = pygame.Surface((60, 60))
    car_image.fill((0, 255, 0))  # 绿色矩形作为替代

# 加载僵尸行走动画帧
zombie_images = []
for i in range(1, 23):  # images/普通/1.png 到 images/普通/22.png
    img_path = os.path.join("images", "普通", f"{i}.png")
    if os.path.exists(img_path):
        image = pygame.image.load(img_path).convert_alpha()
        zombie_images.append(image)
    else:
        print(f"警告: 找不到图片 {img_path}")
        # 如果找不到图片，使用一个替代的表面
        zombie_images.append(pygame.Surface((50, 80)))
        zombie_images[-1].fill((255, 0, 0))  # 红色矩形作为替代

# 检查是否有加载到图片
if not zombie_images:
    print("错误: 没有加载到任何僵尸动画帧")
    pygame.quit()
    sys.exit()


# 定义小车类
class Car:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 60
        self.height = 60
        self.original_x = x  # 记录原始位置
        self.moving = False  # 是否正在移动
        self.speed = 5  # 移动速度
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self):
        # 如果正在移动，向右移动
        if self.moving:
            self.x += self.speed
            self.rect.x = self.x

            # 如果移动到屏幕右侧外，重置位置
            if self.x > SCREEN_WIDTH:
                self.reset()

    def reset(self):
        # 重置小车位置
        self.x = self.original_x
        self.rect.x = self.x
        self.moving = False

    def draw(self):
        if car_image:
            screen.blit(car_image, (self.x, self.y))

    def check_collision(self, zombie):
        """检查是否与僵尸碰撞"""
        if not self.moving and self.rect.colliderect(zombie.rect):
            self.moving = True  # 开始移动
            return True
        return False


# 定义僵尸类
class Zombie:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 50
        self.height = 80
        self.speed = random.randint(1, 3)
        self.current_frame = 0
        self.animation_speed = 5
        self.animation_counter = 0
        self.alive = True
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self):
        if self.alive:
            # 更新位置
            self.x -= self.speed
            # 更新动画
            self.animation_counter += 1
            if self.animation_counter >= self.animation_speed:
                self.current_frame = (self.current_frame + 1) % len(zombie_images)
                self.animation_counter = 0

            # 更新rect位置
            self.rect.x = self.x
            self.rect.y = self.y

            # 如果僵尸走出屏幕左侧，标记为死亡
            if self.x < 0:  # 修改判定条件为 < 0
                self.alive = False

    def draw(self):
        if self.alive:
            screen.blit(zombie_images[self.current_frame], (self.x, self.y))

    def hit(self, pos):
        """检查是否被点击"""
        if self.alive and self.rect.collidepoint(pos):
            self.alive = False
            return True
        return False


# 游戏变量
zombies = []  # 僵尸列表
cars = []  # 小车列表
score = 0  # 积分
zombies_killed = 0  # 击杀的僵尸数量
font = pygame.font.Font(None, 36)  # 字体
spawn_timer = 0  # 僵尸生成计时器
spawn_delay = 120  # 僵尸生成延迟（帧）- 降低出现频率
frame_rate = 60  # 帧率
clock = pygame.time.Clock()

# 创建小车对象
car_positions = [
    (20, 100),
    (20, 200),
    (20, 300),
    (20, 400),
    (20, 500)
]

for pos in car_positions:
    cars.append(Car(pos[0], pos[1]))

# 游戏控制变量
game_over = False  # 游戏结束标志
game_result = ""  # 游戏结果 ("win" 或 "lose")

# 主游戏循环
running = True
while running:
    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
            # 鼠标点击事件
            if event.button == 1:  # 左键点击
                # 检查是否击中僵尸
                for zombie in zombies:
                    if zombie.hit(event.pos):
                        score += 10  # 击中僵尸得10分
                        zombies_killed += 1  # 增加击杀数
                        break  # 一次只能击中一个僵尸

    if not game_over:
        # 生成新僵尸
        spawn_timer += 1
        if spawn_timer >= spawn_delay:
            spawn_timer = 0
            # 随机Y位置生成僵尸
            y_pos = random.randint(100, SCREEN_HEIGHT - 100)
            zombies.append(Zombie(SCREEN_WIDTH, y_pos))

        # 更新小车
        for car in cars:
            car.update()

        # 更新僵尸
        for zombie in zombies[:]:  # 使用副本列表防止迭代时修改
            zombie.update()

            # 检查僵尸是否与小车碰撞
            if zombie.alive:
                for car in cars:
                    if car.check_collision(zombie):
                        zombie.alive = False  # 僵尸被消灭
                        score += 10  # 得分
                        zombies_killed += 1  # 增加击杀数
                        break  # 一次只能被一辆车撞到

            if not zombie.alive:
                # 检查僵尸是否是因为走到最左侧而死亡
                if zombie.x < 0 and zombie in zombies:
                    game_over = True
                    game_result = "lose"
                if zombie in zombies:
                    zombies.remove(zombie)

        # 检查是否击杀了10个僵尸
        if zombies_killed >= 10:
            game_over = True
            game_result = "win"

    # 绘制
    # 绘制背景
    if background_image:
        screen.blit(background_image, (0, 0))
    else:
        screen.fill((135, 206, 235))  # 填充天空蓝背景

    # 绘制小车
    for car in cars:
        car.draw()

    # 绘制僵尸
    for zombie in zombies:
        zombie.draw()

    # 绘制锤子 - 直接使用鼠标位置
    mouse_pos = pygame.mouse.get_pos()
    screen.blit(hammer_image, (mouse_pos[0] - 20, mouse_pos[1] - 20))  # 调整偏移量使锤子尖端对准鼠标

    # 绘制积分和击杀数
    text_surface = font.render(f"jifen: {score}", True, (255, 255, 255))
    killed_text = font.render(f"jisha: {zombies_killed}/10", True, (255, 255, 255))
    screen.blit(text_surface, (10, 10))
    screen.blit(killed_text, (10, 50))

    # 绘制游戏结束信息
    if game_over:
        if game_result == "win":
            # 显示奖杯图片
            if trophy_image:
                screen.blit(trophy_image, (SCREEN_WIDTH // 2 - trophy_image.get_width() // 2, SCREEN_HEIGHT // 2 - 100))
        else:
            # 显示失败图片
            if fail_image:
                screen.blit(fail_image, (SCREEN_WIDTH // 2 - fail_image.get_width() // 2, SCREEN_HEIGHT // 2 - 100))

        # 显示最终积分
        final_score_text = font.render(f"jifen: {score}", True, (255, 255, 255))
        screen.blit(final_score_text, (SCREEN_WIDTH // 2 - final_score_text.get_width() // 2, SCREEN_HEIGHT // 2 + 120))

    # 更新显示
    pygame.display.flip()

    # 控制帧率
    clock.tick(frame_rate)

# 退出pygame
pygame.quit()
sys.exit()



